package commom

import (
	"fmt"
	"time"

	"github.com/shirou/gopsutil/v3/cpu"
	"github.com/shirou/gopsutil/v3/disk"
	"github.com/shirou/gopsutil/v3/mem"
)

//得到CPU比例
func GetCpuPercent() string {
	percent, _ := cpu.Percent(time.Second, false)
	p := fmt.Sprintf("%.2f", percent[0])

	return p
}

//得到内存使用
func GetMemPercent() string {
	memInfo, _ := mem.VirtualMemory()
	p := fmt.Sprintf("%.2f", memInfo.UsedPercent)
	return p
}

//得到磁盘空间，指定文件磁盘空间,保留2位
func GetDiskPercent(dirPath string) string {
	p := ""
	if dirPath == "" {
		parts, _ := disk.Partitions(true)
		diskInfo, _ := disk.Usage(parts[0].Mountpoint)
		p = fmt.Sprintf("%.2f", diskInfo.UsedPercent)
	} else {
		diskInfo, _ := disk.Usage(dirPath)
		p = fmt.Sprintf("%.2f", diskInfo.UsedPercent)
	}
	return p
}
